/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.upms.client;

import com.lambkit.Lambkit;
import com.lambkit.common.service.ServiceManager;
import com.lambkit.module.LambkitModule;
import com.lambkit.module.upms.UpmsConfig;
import com.lambkit.module.upms.api.UpmsApiService;
import com.lambkit.module.upms.api.impl.UpmsApiServiceMock;
import com.lambkit.plugin.rpc.RpcConfig;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2022-09-21
 * @version 1.0
 * @since 1.0
 */
public class UpmsClientModule extends LambkitModule  {

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		if("server".equals(getConfig().getType())) {
			registerLocalService();
		} else if("client".equals(getConfig().getType())) {
			registerRemoteService();
		} 
	}
			
	public void registerLocalService() {
		registerLocalService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerLocalService(String group, String version, int port) {
	}
	
	public void registerRemoteService() {
		registerRemoteService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerRemoteService(String group, String version, int port) {
		ServiceManager.me().remote(UpmsApiService.class, UpmsApiServiceMock.class, group, version, port);
	}
	
	public int getRpcPort() {
		return Lambkit.config(RpcConfig.class).getDefaultPort();
	}
	
	public String getRpcGroup() {
		return Lambkit.config(RpcConfig.class).getDefaultGroup();
	}
	
	public String getRpcVersion() {
		return getConfig().getRpcVersion();
	}
	
	public UpmsConfig getConfig() {
		return Lambkit.config(UpmsConfig.class);
	}
}

